package com.fruit.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class CookieUtil {

	public static String getValue(HttpServletRequest request, String key){
		try{
		Cookie cookies[] = request.getCookies();
		
		if(cookies!=null&&cookies.length>0){
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals(key)) {
					
					return cookie.getValue();
				}
			}
		}
			return "";
		}catch(Exception e){
			return "";
		}
		
	}
	
	public static void setCookie(HttpServletResponse response, String key, String value, int time){
		Cookie cookie = new Cookie(key, value);
		cookie.setMaxAge(60 * 60 * 24 * time);// 设置为15天
		cookie.setPath("/");
		response.addCookie(cookie);
	}
}
